<?php
/**
 * 
 * User/IndexAction.class.php (设计师模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class Zforum_replyAction extends BaseAction
{

	function _initialize()
    {	
		parent::_initialize();
        $_GET =get_safe_replace($_GET);
       
       
        $this->dao = M('User');
        $this->assign('bcid',0);
        $user = $this->dao->find($this->_userid);
        $this->user=$user;

        $this->assign('vo',$user);
		unset($_POST['status']);
		unset($_POST['groupid']);
		unset($_POST['amount']);
		unset($_POST['point']);
        $this->assign('catid',7);

        
    }

    public function index()
    {
        //$this->assign ('catid',7);
       // echo $_SESSION ['authId'];exit;

        $url_s='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        $this->assign('url_s',$url_s);

        $this->Urlrule =F('Urlrule');
        $id =  intval($_REQUEST['id']);

        $plateid =  intval($_REQUEST['plateid']);
        $plate=M('Zforum_plate')->where('id='.$plateid)->find();
        $this->assign ('plate',$plate);


        $field='jg_Zforum_theme.*,jg_user.groupid,jg_user.province,jg_user.city,jg_user.theme_num,jg_user.essence_num,jg_user.reply_num as u_reply_num,jg_user.avatar ';
        $theme=M('Zforum_theme')->field($field)->where('jg_Zforum_theme.id='.$id)->join('jg_user ON jg_Zforum_theme.userid = jg_user.id')->find();
       // print_r($theme);exit;
        $this->assign ('theme',$theme);
        M('Zforum_theme')->where('id='.$id)->setInc('hits'); //添加点击次数
        $seo_title = $theme['title'].'-'.$theme['catname'];
        $this->assign ('seo_title',$seo_title);

        $p= max(intval($_REQUEST[C('VAR_PAGE')]),1);

         $where = "pid=0 and theme_id=$id ";
            // echo $where;exit;
         if($_GET['userid']){
             $where .= "and userid=".$_GET['userid'];

         }
            //print_r($plate);exit;
            $this->dao= M('Zforum_reply');
            $count = $this->dao->where($where)->count();
            //echo $this->dao->getLastSql();exit;

            if($count){
                import ( "@.ORG.Page" );
                $listRows =10;

                $page = new Page ( $count, $listRows );
                $pages = $page->preshow();
                $field = '*';
                $order='id asc';
                if($_GET['order']){
                    $order=$_GET['order'].' desc';
                }

               // $list = $this->dao->field($field)->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
                //echo $this->dao->getLastSql();exit;
                $field='jg_Zforum_reply.*,jg_user.groupid,jg_user.province,jg_user.city,jg_user.theme_num,jg_user.essence_num,jg_user.reply_num,jg_user.avatar ';
                  if(empty($_GET['p'])){
                      $page->listRows=9;

                  }
                $list=$this->dao->field($field)->where($where)->join('jg_user ON jg_Zforum_reply.userid = jg_user.id')->order('jg_Zforum_reply.id asc')->limit($page->firstRow . ',' . $page->listRows)->select();
                //echo $this->dao->getLastSql();exit;
               // print_r($list);exit;


                $this->assign('pages',$pages);
                $this->assign('list',$list);
            }


        $_SESSION['rhemeurl']=$_SERVER['REQUEST_URI'];

        // echo  $_SESSION['rhemeurl'];exit;

        $this->display();
    }



    public function insert()
    {

        if(!$this->_userid){

            $this->assign('jumpUrl',U('User/Login/index'));
            $this->error(L('nologin'));
            exit;
        }

        $content=trim (str_replace('&nbsp;','',$_POST['reply']['content']));

        //$content=trim($content);
        if(empty($content)){
           // echo $content;exit;
            $this->error(L('请输入内容'));
        };
       // echo $content.'2222';exit;
         if($_POST['reply_uname']['pid']){
             $l=mb_strlen($_POST['reply']['content']);
            if($l>450){
                $this->error(L('回复长度超过限制'));
            }
         };

        $theme_id=$_REQUEST['reply']['theme_id'];
        $where = "pid=0 and theme_id= $theme_id ";
        $db=M('Zforum_reply');
        $count = $db->where($where)->count();

        $db->create($_POST['reply']);
        $db->createtime = time();
        $db->floor=$count+1;//楼层
        $db->userid=$this->_userid;
        $db->username=$this->user['username'];
        $result	=$db->add();
        if($result){
           if($_POST['mes'][userid]!=$this->_userid){
            $mes=M('Zforum_message');
            $mes->create($_POST['mes']);
            $mes->reply_uid=$this->_userid;
            $mes->reply_uname=$this->user['username'];
            $mes->createtime = time();
            $mes->content = $content;
            $mes->add();//添加提醒消息
             }
            M('Zforum_theme')->where("id=".$theme_id)->setInc('reply_num'); //添加帖子回复次数
            $data['updatetime']=time();
            M('Zforum_theme')->where("id=".$theme_id)->save($data);//修改帖子最后回复时间
            M('User')->where("id=".$this->_userid)->setInc('reply_num'); //添加个人回复次数
            $this->success(L('回复成功'),$_SESSION['rhemeurl']);
        }

    }


    public function add()
    {
        $themeid=$_REQUEST['theme_id'];
        $theme=M('Zforum_theme')->where('id='.$themeid)->find();
        $this->assign('theme',$theme);

        $reply_id=$_REQUEST['pid'];
        $reply=M('Zforum_reply')->where('id='.$reply_id)->find();
        $this->assign('reply',$reply);
        $re_name=$_REQUEST['re_name'];
        $this->assign('re_name',$re_name);

        $this->display();
    }
  //楼中楼分页
public function ajaxpage(){

    $count = M('Zforum_reply')->where('pid='.$_GET['pid'])->count();
    import ( "@.ORG.Page" );
    $listRows =8;
    $page = new Page ( $count, $listRows );
    $pages = $page->ajaxshow($_GET['pid']);
    $field='jg_Zforum_reply.*,jg_user.avatar ';
    $result=M('Zforum_reply')->field($field)->where('pid='.$_GET['pid'])->join('jg_user ON jg_Zforum_reply.userid = jg_user.id')->order('jg_Zforum_reply.id asc')->limit($page->firstRow . ',' . $page->listRows)->select();
   // echo M('Zforum_reply')->getLastSql();exit;
    $arr_msg=array('pages'=>$pages,'result'=>$result,'sssawc'=>$_SESSION ['authId']);

    echo json_encode($arr_msg);//转换格式


}



}

